package com.cbgolf.oa.activity.statistics;

import android.content.Intent;
import android.os.Bundle;
import com.cbgolf.oa.R;
import com.cbgolf.oa.activity.BaseNewActivity;
import com.cbgolf.oa.contract.IListStatisticsContract.IListStatisticsPresenter;
import com.cbgolf.oa.contract.IListStatisticsContract.IListStatisticsView;
import com.cbgolf.oa.contract.IListStatisticsContract.IListStatisticsWaiter;
import com.cbgolf.oa.entity.ListBean;
import com.cbgolf.oa.entity.StatisticsBean;
import com.cbgolf.oa.event.EventStatistics;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.presenter.ListStatisticsPresenterImp;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.views.ListStatisticsViewImp;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 五种统计列表（book,business,bill,caddie,player)
 *
 * @author lq
 */
public class ListStatisticsActivity extends BaseNewActivity implements IListStatisticsWaiter {

  private IListStatisticsPresenter presenter;
  private IListStatisticsView view;

  private int mType;
  private StatisticsBean mData;

  @Override
  protected int getLayoutId() {
    return R.layout.a_recycler_list;
  }

  @Override
  protected void init(Bundle savedInstanceState) {
    EventBus.getDefault().register(this);
    mType = DataUtil.getIntExtra(getIntent(), DataManager.KEY_TYPE, 0);
    mData = (StatisticsBean) DataUtil.getSerializableExtra(getIntent(), DataManager.KEY_DATA);
    view = new ListStatisticsViewImp(this, this, mType, mData);
    presenter = new ListStatisticsPresenterImp(view);
    if (mType != DataManager.STATISTICS_CADDIE_ALL) {
      presenter.requestData(mType, mData, DataUtil.INITDATA);
    } else {
      presenter.filterCaddieData(null != mData && !Util.isNull(mData.barQueryName) ?
          mData.barQueryName.replace("级", "") : "", mData, DataUtil.INITDATA);
    }


  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
  }

  @Subscribe(threadMode = ThreadMode.MAIN)
  public void requestOver(EventStatistics event) {
    view.requestOver(event);
  }

  @Override
  public String getActivityTitle() {
    if (null != mData) {
      return mData.barQueryName;
    }
    return null;

  }

  @Override
  public void back() {
    finish();
  }

  @Override
  public void loadMoer() {
    presenter.requestData(mType, mData, DataUtil.LOADMORE);
  }

  @Override
  public void refresh() {
    presenter.requestData(mType, mData, DataUtil.REFRESH);
  }

  @Override
  public void showDetails(ListBean data) {
    if (null != data && null != mData) {
      data.endTime = mData.endTime;
    }
    mStartActivity(new Intent(context, StatisticsListDetailsActivity.class)
        .putExtra(DataManager.KEY_DATA, data)
        .putExtra(DataManager.KEY_TYPE, mType));
  }

  @Override
  public void requestData(int dataType, StatisticsBean data, int requestEvent) {
    presenter.requestData(dataType, data, requestEvent);
  }

  @Override
  public void filterCaddieData(String level, StatisticsBean data, int requestEvent) {
    presenter.filterCaddieData(level, data, requestEvent);
  }


}

